<template>
  <div class="panel-header">
    <div class="panel-title">
      <i class="panel-icon status-icon"></i>
      <span>设备运行状态</span>
    </div>
    <div class="panel-controls">
      <el-select v-model="selectedEquipment" placeholder="选择设备" clearable size="small" @change="onEquipmentChange">
        <el-option
          v-for="item in equipmentList"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </div>
  </div>
  <div class="panel-body">
    <div ref="chartRef" class="chart-container"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  equipmentList: {
    type: Array,
    default: () => []
  },
  initialSelectedEquipment: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update:selectedEquipment'])

const chartRef = ref(null)
const chart = ref(null)
const selectedEquipment = ref(props.initialSelectedEquipment)

// 监听设备选择变化
watch(() => selectedEquipment.value, (newValue) => {
  emit('update:selectedEquipment', newValue)
  updateChart()
})

// 监听设备列表变化
watch(() => props.equipmentList, () => {
  updateChart()
}, { deep: true })

// 初始化图表
const initChart = () => {
  if (!chartRef.value) return

  chart.value = echarts.init(chartRef.value)

  // 设置图表响应式
  const resizeHandler = () => {
    chart.value && chart.value.resize()
  }
  window.addEventListener('resize', resizeHandler)

  // 组件卸载时移除事件监听
  onBeforeUnmount(() => {
    window.removeEventListener('resize', resizeHandler)
    chart.value && chart.value.dispose()
  })

  updateChart()
}

// 更新图表数据
const updateChart = () => {
  if (!chart.value) return

  // 模拟数据
  const option = {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 10,
      data: ['运行', '停机', '报警', '离线']
    },
    series: [
      {
        name: selectedEquipment.value || '设备状态',
        type: 'pie',
        radius: ['50%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 16,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: [
          { value: 735, name: '运行', itemStyle: { color: '#67C23A' } },
          { value: 310, name: '停机', itemStyle: { color: '#909399' } },
          { value: 234, name: '报警', itemStyle: { color: '#F56C6C' } },
          { value: 135, name: '离线', itemStyle: { color: '#E6A23C' } }
        ]
      }
    ]
  }

  chart.value.setOption(option)
}

// 处理设备选择变化
const onEquipmentChange = (value) => {
  selectedEquipment.value = value
}

onMounted(() => {
  initChart()
})
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 100%;
}
</style>
